home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_10_06
/
1006032a
< prev
next >
Wrap
Text File
|
1992-04-12
|
1KB
|
51 lines
#include <stdarg.h> /* variable argument macros */
#include <stdio.h> /* for printf */
int maxn(int n, ...); /* the function prototype for maxn */
/*
** Main makes some calls to maxn, and prints the results. */
*/
void main(void)
{
printf("MaxN tests:\n\n");
printf(
"maxn(2, 3, 5) = %d\n",
maxn(2, 3, 5) );
printf(
"maxn(5, 3, 5, 7, 11, 13) = %d\n",
maxn(5, 3, 5, 7, 11, 13) );
printf(
"maxn(3, 3, 5, 7, 11, 13) = %d\n",
maxn(3, 3, 5, 7, 11, 13) );
printf(
"maxn(1, 9) = %d\n",
maxn(1, 9) );
}
/*
** The maxn function returns the largest of n integer arguments.
*/
int maxn(int n, ...)
{
int val, max_val;
va_list argp; /* pointer to arguments */
va_start(argp, n); /* points argp to arg after n */
max_val = va_arg(argp, int); /* get first of n arguments */
while (--n)
{
val = va_arg(argp, int); /* get subsequent arguments */
if (val > max_val)
max_val = val; /* max_val gets the largest */
}
va_end(argp); /* clean up */
return max_val; / return value of the largest */
}